package com.example.springmybatisservice.service.impl;
import com.example.springmybatisservice.PageRequest;
import com.example.springmybatisservice.User;
import com.example.springmybatisservice.mapper.UserMapper;
import com.example.springmybatisservice.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author wuxinxin
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    ExecutorService executorService=Executors.newFixedThreadPool(30);


    /**
     * 使用pageHelp进行分页，引入第三方插件
     * @return
     */
    @Override
    public PageInfo<User> list(PageRequest pageRequest) {
        //支持和page对象自动映射，方便我们自己定义pageRequest
        PageHelper.startPage(pageRequest);

        List<User> users = userMapper.selectByUserId(1);

        PageInfo<User> pageInfo=new PageInfo<>(users);
        return pageInfo;
    }

    @Override
    public void addTestData() {

        /*//查询最大的user_id
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.select("max(id) id");
        User user = userMapper.selectOne(userQueryWrapper);

        //查询最大记录
        User maxUser = userMapper.selectByPrimaryKey(user.getId());

        int id=maxUser.getId();

        Random random = new Random();

        for(int i=maxUser.getUserId()+1;i<maxUser.getUserId()+1+50000;i++){

            for(int j=0;j<random.nextInt(20);j++){
                User temp = new User();
                temp.setId(++id);
                temp.setUserId(i);
                temp.setChildrenId(random.nextInt(1000));
                executorService.submit(()->{
                    userMapper.insertSelective(temp);
                });
            }
        }*/
    }

    @Override
    public List<User> getOne(Integer userId) {
        return userMapper.selectByUserId(userId);
    }
}
